package ui;

import entity.TableName;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class QueryTableFun {
    private DataTable dt;
    private String queryCode;
    private String queryName;

    public QueryTableFun(DataTable dt,String queryCode, String queryName){
        this.dt = dt;
        this.queryCode = queryCode;
        this.queryName = queryName;
    }

    public void paddingTableData(DataTable dt,String queryCode, String queryName) throws Exception {
        System.out.println("查询编码为：" + queryCode + "，查询名称或者缩写为:" + queryName);

        InputStream inputStream = this.getClass().getResourceAsStream("/file/表名.csv");
        InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");

        BufferedReader br = new BufferedReader(isr);
        String line = null;
        ArrayList<TableName> tableList = new ArrayList<>();

        Vector<Vector<String>> tableData = this.dt.getTableData();
        tableData.clear();
        List<TableName> tableNameList = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            String replacedLine = line.replace("\"","");
            String[] lineUnit = replacedLine.split(",",3);

            TableName tableName = new TableName();
            tableName.setTableName(lineUnit[0]);
            tableName.setTableType(lineUnit[1]);
            tableName.setComments(lineUnit[2]);
            tableNameList.add(tableName);
        }

        List<TableName> aimList = new ArrayList<>();
        String suffix = getTableSuffix(queryCode,queryName);
        Pattern pattern = null;
        if(suffix != null){
            pattern = Pattern.compile(suffix);
            for(int i=0; i < tableNameList.size(); i++){
                Matcher matcher = pattern.matcher((tableNameList.get(i)).getTableName());
                if(matcher.find()){
                    aimList.add(tableNameList.get(i));
                }
            }
        }else if(suffix == null && queryName != null && StringUtils.isBlank(queryCode)){
            pattern = Pattern.compile(queryName);
            for(int i=0; i < tableNameList.size(); i++){
                Matcher matcher = pattern.matcher((tableNameList.get(i)).getComments());
                if(matcher.find()){
                    aimList.add(tableNameList.get(i));
                }
            }
        }else{
            throw new RuntimeException("找不到查询内容！");
        }

        if(aimList.size() > 0){
            for(int j=0;j<aimList.size();j++){
                TableName tableName = aimList.get(j);
                String acturalSuffix = findCodeBySuffix(tableName.getTableName().substring(0,4));

                Vector<String> vecUnit = new Vector<>();
                if(StringUtils.isNotEmpty(queryCode)){
                    vecUnit.add(queryCode);
                }else{
                    vecUnit.add(acturalSuffix);
                }
                vecUnit.add(tableName.getComments());
                vecUnit.add(tableName.getTableName());
                tableData.add(j,vecUnit);
                this.dt.updateUI();
            }
        }else{
            throw new RuntimeException("找不到查询内容！");
        }
    }

    public String getTableSuffix(String code,String queryName){
        String returnValue = null;
        if(StringUtils.isNotBlank(code)){
            switch(code){
                case "110":
                    returnValue = "TPBB";
                    break; //可选
                case "120" :
                    returnValue = "TPBB";
                    break; //可选
                case "130" :
                    returnValue = "TPBB";
                    break; //可选
                case "210" :
                    returnValue = "TPBL";
                    break; //可选
                case "220" :
                    returnValue = "TPBC";
                    break; //可选
                case "230" :
                    returnValue = "TPBG";
                    break; //可选
                case "310" :
                    returnValue = "TEBB";
                    break; //可选
                case "410" :
                    returnValue = "TEBL";
                    break; //可选
                case "510" :
                    returnValue = "TMBM";
                    break; //可选
                case "420" :
                    returnValue = "TEBC";
                    break; //可选
                case "440" :
                    returnValue = "TEBG";
                    break; //可选
                case "610" :
                    returnValue = "TEBF";
                    break; //可选
                case "620" :
                    returnValue = "TEBF";
                    break; //可选
                case "630" :
                    returnValue = "TEBF";
                    break; //可选
            }
        }else{
            switch(queryName){
                case "jd":
                    returnValue = "T[EP]BL";
                    break; //可选
                case "借贷":
                    returnValue = "T[EP]BL";
                    break; //可选
                case "grjd":
                    returnValue = "TPBL";
                    break; //可选
                case "qyjd":
                    returnValue = "TEBL";
                    break; //可选
                case "gr":
                    returnValue = "TPBB";
                    break; //可选
                case "个人":
                    returnValue = "TPBB";
                    break; //可选
                case "qy":
                    returnValue = "TEBB";
                    break; //可选
                case "企业":
                    returnValue = "TEBB";
                    break; //可选
                case "jz":
                    returnValue = "TPBB";
                    break; //可选
                case "家族":
                    returnValue = "TPBB";
                    break; //可选
                case "grzj":
                    returnValue = "TPBB";
                    break; //可选
                case "个人证件":
                    returnValue = "TPBB";
                    break; //可选
                case "sx":
                    returnValue = "T[EP]BC";
                    break; //可选
                case "授信":
                    returnValue = "T[EP]BC";
                    break; //可选
                case "grsx":
                    returnValue = "TPBC";
                    break; //可选
                case "个人授信":
                    returnValue = "TPBC";
                    break; //可选
                case "qysx":
                    returnValue = "TEBC";
                    break; //可选
                case "企业授信":
                    returnValue = "TEBC";
                    break; //可选
                case "db":
                    returnValue = "T[EP]BG";
                    break; //可选
                case "担保":
                    returnValue = "T[EP]BG";
                    break; //可选
                case "grdb":
                    returnValue = "TPBG";
                    break; //可选
                case "个人担保":
                    returnValue = "TPBG";
                    break; //可选
                case "qydb":
                    returnValue = "TEBG";
                    break; //可选
                case "企业担保":
                    returnValue = "TEBG";
                    break; //可选
                case "dzy":
                    returnValue = "T[EPM]BM";
                    break; //可选
                case "抵质押":
                    returnValue = "T[EPM]BM";
                    break; //可选
                case "cb":
                    returnValue = "T[EP]BF";
                    break; //可选
                case "财报":
                    returnValue = "T[EP]BF";
                    break; //可选
//                case "sydw":
//                    returnValue = "T[EP]BF";
//                    break; //可选
//                case "事业单位":
//                    returnValue = "T[EP]BF";
//                    break; //可选
            }
        }
        return returnValue;
    }

    public String findCodeBySuffix(String suffix){
        String returnValue = null;
        switch(suffix){
            case "TPBB":
                returnValue = "110 || 120 || 130";
                break; //可选
            case "TPBL" :
                returnValue = "210";
                break; //可选
            case "TPBC" :
                returnValue = "220";
                break; //可选
            case "TPBG" :
                returnValue = "230";
                break; //可选
            case "TEBB" :
                returnValue = "310";
                break; //可选
            case "TEBL" :
                returnValue = "410";
                break; //可选
            case "TMBM" :
                returnValue = "510";
                break; //可选
            case "TEBC" :
                returnValue = "420";
                break; //可选
            case "TEBG" :
                returnValue = "440";
                break; //可选
            case "TEBF" :
                returnValue = "610 || 620 || 630";
                break; //可选
        }
        return returnValue;
    }



}
